
*Set path
global raw "~/agriculture_temperature"
insheet using $raw/data/seasurface_temperature/coastal_sea_surface_daily1995_2015.csv, clear
drop if month==2 & day==29
*gen temperature bin	
	* 5C bin	
	forvalues b = 0(5)45  {
		qui gen dd5bin`b' = 0
		qui replace dd5bin`b' = 1 if sst >= `b' & sst < `b'+5
		}			
 collapse (sum) dd5bin*, by(pro_name county_name county_code year)
	replace dd5bin35 = dd5bin35 + dd5bin40 + dd5bin45
	drop dd5bin40 dd5bin45
***change the county code 
rename county_code county_code2019
merge m:1 county_code2019  using $raw/data/county2019_2015县代码变更.dta
keep if _merge==3
drop _merge 
drop county_code2019 county_name_2019
*save
save $raw/data/seasurface_temperature/coastal_sea_surface_year1995_2015.dta, replace


*replace the county code in 2015 with county code in 2005
global raw "~/agriculture_temperature"
use $raw/data/seasurface_temperature/coastal_sea_surface_year1995_2015, clear
rename county_code2015 county_code
* match with the updated county code in 2005
merge m:1 county_code using $raw/data/county_code_2015_2005_new.dta
drop if _merge==2
drop _merge
replace county_code_2005 = county_code if county_code_2005 ==.

drop county_code
rename county_code_2005 county_code
***rename 
	forvalues t = 0(5)35 {
		rename dd5bin`t' sea_surface_dd5bin`t'
		}

save $raw/data/seasurface_temperature/coastal_sea_surface_year1995_2015_county2005.dta, replace

********************************************************************************
*Sea Surface Temperature Effects on the Fishery Sector
*Figure E1
********************************************************************************
use $raw/data/merged_for_reg.dta, clear
rename ctycode county_code
merge 1:1 county_code year using $raw/data/seasurface_temperature/coastal_sea_surface_year1995_2015_county2005.dta
drop if _merge==2
drop _merge
xtset county_code year

* add negative degree bins

	egen sea_surface_dd5binPos=rowtotal(sea_surface_dd5bin*)

	gen sea_surface_dd5binNeg = 365 -  sea_surface_dd5binPos
	replace sea_surface_dd5binNeg = 0 if sea_surface_dd5binNeg < 0
	drop sea_surface_dd5binPos
	replace sea_surface_dd5bin30 = sea_surface_dd5bin30 + sea_surface_dd5bin35
	drop sea_surface_dd5bin35
*set reference bin
	rename sea_surface_dd5bin10 REF_BIN

	estimates clear
	
	foreach x in seawater_catch seawater_fish {
		
		qui reghdfe ln_`x' sea_surface_dd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==1,  ///
			absorb(county_code year) cluster(county_code proXyear) 
			estimates store n_`x'
			
		qui reghdfe ln_`x' sea_surface_dd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==2,  ///
			absorb(county_code year) cluster(county_code proXyear) 
			estimates store s_`x'		
			}	

	*north
	esttab n_*, b(4) se(4) keep(sea_surface_dd5bin*) brackets	
	*south
	esttab s_*, b(4) se(4) keep(sea_surface_dd5bin*) brackets	
